home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_300
/
323_01
/
explod.c
< prev
next >
Wrap
Text File
|
1990-08-04
|
31KB
|
965 lines
/****************************************************************************
* EXPLOD.C - simple fireworks for Hercules/CGA/EGA
*
* This program was developed with Datalight C 2.20 and Arrowsoft ASM 1.00d.
* (It also works with Turbo C 1.5/MASM 5.0 with one EQU change in EXPA.ASM).
* The code should be compilable by most other C compilers. However, the
* segment names in EXPA.ASM will have to be changed to suit the compiler.
*
* To compile:
* asm expa ; OR masm /mx expa ;
* dlc explod.c expa.obj (don't forget the /mx !!!)
* tcc explod.c expa.obj
* (C) 1989 Dennis Lo
* You are free to use and distribute this source code, provided that the
* authors' names remain in the code, and that both the source and executables
* of modified versions are clearly distinguished from the originals with
* descriptive messages.
*
* Change Log
* 89/06/24 Dennis Lo V1.0 Initial release.
* 89/07/03 Dennis Lo V1.1 Added cmd line options. Code cleanups.
* 89/07/26 Erik Liljencrantz Added EGA support with autodetection
* Restores previous CRT textmode on exit
* 89/08/19 Dennis Lo V1.2 Code cleanups. Changed references to frames
* table to use segment and offsets so that multiple
* frames tables can be supported in the future
* without switching to a large data memory model.
* 89/09/03 Erik Liljencrantz Added VGA support with autodetection
* 90/01/20 Dennis Lo V2.0 Allow multiple explosion frame tables.
* Read in explosion shape tables from data files.
* Added -f and keypress options. Split part of
* functionality into expgen.c. Keyboard control.
* -n option.
****************************************************************************/
#include <stdio.h>
#include <ctype.h>
#ifndef DLC
# include <dir.h>
#endif
#include <dos.h>
#include "explod.h"
#define PUT(s) fputs(s,stdout)
/*=====================================================================
* External assembler routines.
*=====================================================================
*/
/* Datalight C does not prepend underscores to external names */
#ifdef DLC
# define DosAlloc _DosAlloc
# define DosFree _DosFree
# define DosMemLeft _DosMemLeft
# define ChkKey _ChkKey
# define gr_card _gr_card
# define gr_setcard _gr_setcard
# define gr_gmode _gr_gmode
# define gr_tmode _gr_tmode
# define gr_addr _gr_addr
# define gr_store_pt _gr_store_pt
# define gr_frplot _gr_frplot
#endif
extern unsigned short DosAlloc();
extern unsigned short DosFree();
extern unsigned short DosMemLeft();
extern int ChkKey();
extern int gr_card();
extern gr_setcard();
extern gr_gmode();
extern gr_tmode();
extern int gr_addr();
extern gr_store_pt();
extern gr_frplot();
#define DEADPOINT 32767 /* this must match the definition in expa.asm */
/*=====================================================================
* Configuration Parameters
*=====================================================================
*/
/* Video parameters */
static int Interlace_factor;
static int Screen_xsize;
static int Screen_ysize;
/* Animation parameters */
static int Simul_exps = -1; /* # of simultaneous explosions */
static int Delay_factor = 0; /* amount to delay after each frame */
/* Explosion parameters */
static int Gravity = 0; /* Gravity override */
static int Wind = 0; /* Wind override */
static int Centre_x; /* Coords of centre of screen */
static int Centre_y;
static unsigned int Centre_y_mask;
/* Explosion control parameters */
static int Leave_trail = 0; /* If non-0 then don't erase explosions */
static int Event_limit = 0; /* # of exp events before exiting */
static int Total_frames = 0; /* used to find avg #frames per explosion */
/*=====================================================================
* Explosion Creation table:
* Temporarily keeps track of the parameters and points of an explosion
* while its frames are being calculated.
*=====================================================================
*/
/* 16.16 fixed point type: overlays a 32-bit long with its 16-bit
whole number part and its 16-bit fractional part */
typedef union
{
long l;
struct /* Note: this is endian dependent */
{
unsigned short ls;
short ms;
} s;
} fixed;
/* Structure of an explosion point at explosion creation */
typedef struct
{
fixed x, y; /* location */
fixed xv, yv; /* velocity */
fixed xa, ya; /* acceleration */
int alive; /* liveness: 0=dead */
} exp_pt_type;
/* Explosion creation table */
exp_pt_type Pt_table [MAX_POINTS+1];
/*=====================================================================
* Explosion type descriptor - contains all information in a ready-to-
* playback explosion, including a pointer to its frames table. The
* frames table contains an array of MAX_FRAMES frames. Each frame is
* one frame in the animation of the explosion, and consists of
* MAX_POINTS structures of the form {offset (2 bytes), value (1 byte)}.
* The frame table structure treated only as a block of memory by the
* C code, so it is not explicitly declared. (It cannot be declared
* in C in a portable way due to structure alignment padding.)
*=====================================================================
*/
#define FRAME_PT_SIZE 3 /* size of one point in the frame table */
#define MAX_EXP_DESC 30 /* Max # of explosion types */
/* Explosion descriptor type */
typedef struct
{
int nframes;
int npoints;
int xsize;
int ysize;
int gravity;
int wind;
int trail_len;
unsigned short frame_seg;
unsigned short frame_ofs;
} exp_desc_type;
/* Array of explosion descriptors for the explosions read in from disk */
static exp_desc_type Exp_desc [MAX_EXP_DESC];/* Explosion descriptors */
static int Num_exp_desc; /* # of explosions read in */
/*=====================================================================
* Explosion Event Table - each entry controls one instance of an
* active explosion event during playback.
*=====================================================================
*/
#define MAX_EXPS 48 /* max # of simultaneously active explosion events */
/* Explosion events table */
typedef struct
{
int centre; /* addr of centre of explosion */
int frame_num; /* frame #. Dead if -1 */
exp_desc_type *exp_desc; /* explosion type */
} exp_event_type;
static exp_event_type Exp_ev_tab [MAX_EXPS];
/*====================================================================
* Return a random number between 0..maxval
*====================================================================
*/
int
rnd (maxval)
int maxval;
{
# define MAX_RAND 32767 /* max val returned by rand() */
long l;
l = (long) maxval * rand() / MAX_RAND;
return ((int) l);
}
/*====================================================================
* Read a number from a string - replaces the need for sscanf() which
* reduces the size of the executable by about 2.5K.
* Returns a ptr to the next word in the string.
*====================================================================
*/
char *
str_to_num (str, num)
char *str; /* IN: ptr to string containing the number */
int *num; /* OUT: the number read */
{
/* Skip leading spaces */
while (*str && *str == ' ') str++;
*num = atoi (str);
/* Skip non spaces (skip past the number) */
while (*str && *str != ' ') str++;
return (str);
}
/*====================================================================
* Local memory allocator:
* MemInit() - allocates as much memory from DOS as possible.
* Return 1 if successful, 0 if not.
* MemAlloc() - allocates a block of memory to the caller.
* Returns segment addr of block, or 0 if failed.
* MemCleanup() - returns all memory